<?php
namespace app\controller;

use think\Request;
use think\facade\Db;

class Useradmin extends Common
{

	/**
	 * 查询数据
	 *
	 * @return
	 */
	public function read(Request $request) {
		if($request->sessInfo['level'] <> 1) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$limit = $request->param('limit/d', 20);
		$sort = $request->param('sort', 'id', 'trim');
		$order = $request->param('order', 'desc', 'trim');

		array_push( $where, ['level', '=', 2] );

		$username = $request->param('username', '', 'trim');
		if($username) {
			array_push( $where, ['username', 'like',  "%{$username}%" ] );
		}

		$nickname = $request->param('nickname', '', 'trim');
		if($nickname) {
			array_push( $where, ['nickname', 'like',  "%{$nickname}%" ] );
		}

		$company = $request->param('company', '', 'trim');
		if($company) {
			array_push( $where, ['company', 'like',  "%{$company}%" ] );
		}

		$query = Db::name('cfg_user')->where($where)->order($sort, $order)->paginate($limit);

		$items = $query->items();
		foreach($items as &$value) {
			$value['password'] = null;
			$value['roles'] = Db::table('cfg_role')->field('id, roleName')->where('id', $value['role'])->select()->toArray();
		}

		return ['code'=> 0, 'msg'=> '', 'count'=> $query->total(), 'data'=> $items ];
	}

	/**
	 * 更新数据
	 *
	 * @return
	 */
	public function update(Request $request) {
		if($request->sessInfo['level'] <> 1) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$param = $request->param();
		if( isset($param['state']) ) {
			$status = $request->param('status', '', 'trim');
			Db::name('cfg_user')
					->where('id', $param['state'])
					->update([ 'status' => $status]);
			return ['code'=>0, 'msg'=>'操作成功'];
		}

    $id = $request->param('id', '', 'trim');
		$company = $request->param('company', '', 'trim');
		$companyId = $request->param('companyId', '', 'trim');
		$domainname = $request->param('domainname', '', 'trim');
		$expires = $request->param('expires', '', 'trim');
		$nickname = $request->param('nickname', '', 'trim');
		$password = $request->param('password', '', 'trim');
		$username = $request->param('username', '', 'trim');
		$usersnum = $request->param('usersnum', '', 'trim');
		if($password) {
			$password = password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]);
			Db::name('cfg_user')
					->where('id', $id)
					->update([ 'password' => $password]);
		}
		Db::name('cfg_user')
				->where('id', $id)
				->update([ 'company' => $company, 'domainname' => $domainname, 'expires' => date('Y-m-d H:i:s', strtotime($expires)), 'nickname' => $nickname, 'usersnum' => $usersnum]);
		return ['code'=>0, 'msg'=>'操作成功'];
	}

	/**
	 * 创建数据
	 *
	 * @return
	 */
	public function create(Request $request) {
		if($request->sessInfo['level'] <> 1) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$company = $request->param('company', '', 'trim');
		$companyId = $request->param('companyId', '', 'trim');
		$domainname = $request->param('domainname', '', 'trim');
		$expires = $request->param('expires', '', 'trim');
		$nickname = $request->param('nickname', '', 'trim');
		$password = $request->param('password', '', 'trim');
		$username = $request->param('username', '', 'trim');
		$usersnum = $request->param('usersnum', '', 'trim');
		$query = Db::table('cfg_user')->where('companyId', $companyId)->value('companyId');
		if(!empty($query)) {
			return ['code'=>403, 'msg'=>'操作失败, 企业ID已存在!'];
		}
		$query = Db::table('cfg_user')->where('username', $username)->value('username');
		if(!empty($query)) {
			return ['code'=>403, 'msg'=>'操作失败, 用户已存在!'];
		}

		$query = Db::table('cfg_system')->where('companyId', $companyId)->value('id');
		if( empty($query) ) {
			Db::name('cfg_system')->insert(['companyId' => $companyId, 'baiduocpc' => 10, 'baiduexocpc' => 10]);
		}

		file_put_contents(root_path('data') . md5($companyId . 'site' ), 'a:3:{s:8:"sitename";s:24:"神游竞价管理系统";s:11:"siteversion";s:5:"22222";s:14:"sitestatistics";s:3:"333";}');

		file_put_contents(root_path('data') . md5($companyId . 'ocpc'), 'a:2:{s:9:"baiduocpc";s:2:"10";s:11:"baiduexocpc";s:2:"10";}');

		file_put_contents(root_path('data') . md5($companyId . 'spider' ), 'a:1:{s:14:"interceptingip";s:822:"180.153.232
180.153.234
180.153.236
180.163.220
112.34.110
14.215.176
42.236.101
42.236.102
42.236.103
42.236.10
42.236.12
42.236.13
42.236.14
42.236.15
42.236.16
42.236.17
42.236.46
42.236.48
42.236.49
42.236.50
42.236.51
42.236.52
42.236.53
42.236.54
42.236.55
42.236.99
61.135.186
123.125.71
111.206.221
180.76.15
180.149.133
220.181.32
36.110.199
124.166.232
116.179.32
180.97.35
61.135.189
123.126.113
111.202.100
36.110.147
220.181.125
106.120.173
49.7.20
218.30.103
106.38.241
111.13.94
58.250.125
183.36.114
42.156.138
42.120.236
106.11.159
65.55.210
13.66.139
157.55.39
207.46.13
40.77.167
52.247.2
203.208.60
74.125.210
72.14.199
64.233.172
66.249.64
66.102.6
111.225.149
220.243.135
110.249.201
60.8.151
220.249.46
106.39.246
211.94.109
222.137.12
116.198.10
114.118.6
60.10.84
223.104.103
113.142.72
111.206.36";}');

		$password = password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]);
		Db::name('cfg_user')->insert([ 'companyId' => $companyId, 'company' => $company, 'level' => 2, 'status' => 1,
			'domainname' => $domainname, 'expires' => date('Y-m-d H:i:s', strtotime($expires)), 'nickname' => $nickname, 'password' => $password,
			'username' => $username, 'usersnum' => $usersnum, 'datatime' => date('Y-m-d H:i:s')]);
		return ['code'=>0, 'msg'=>'操作成功'];
	}

	/**
	 * 删除数据
	 *
	 * @return
	 */
	public function deleteCompanyId($companyId) {
		Db::execute(" DELETE FROM cfg_access where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_adgroup where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_area where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_bazaar where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_campaign where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_copy where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_creative where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_expand where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_hours where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_logs where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_optlog where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_role where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_search where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_service where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_service_analyse where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_service_internal where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_service_plan where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_site where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_smslog where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_system where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_update_time where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_user where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_xhs where companyId = '{$companyId}' ");
		Db::execute(" DELETE FROM cfg_zone where companyId = '{$companyId}' ");
	}

	/**
	 * 删除数据
	 *
	 * @return
	 */
	public function delete(Request $request) {
		if($request->sessInfo['level'] <> 1) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$param = $request->param();
		if( !isset($param['id']) ) {
			return ['code'=> 403, 'msg'=> '操作失败!'];
		}
		$explode = explode(',', $param['id']);
		foreach( $explode as $value ) {
			$companyId = Db::table('cfg_user')->where('id', $value)->value('companyId');
			if( $companyId ) {
				$this->deleteCompanyId($companyId);
			}
		}
		return ['code'=> 0, 'msg'=> '删除成功!'];
	}

	/**
	 * 查询用户
	 *
	 * @return
	 */
	public function query(Request $request)
	{
		$companyId = $request->param('companyId', '', 'trim');
		if( $companyId ) {
			$query = Db::table('cfg_user')->where('companyId', $companyId)->value('companyId');
			if(empty($query)){
				return json(['code'=>0, 'data' => [], 'msg'=>'']);
			}
			return json(['code'=>0, 'data' => [['companyId' => $companyId]], 'msg'=>'']);
		}
		$username = $request->param('username', '', 'trim');
		if( $username ) {
			$query = Db::table('cfg_user')->where('username', $username)->value('username');
			if(empty($query)){
				return json(['code'=>0, 'data' => [], 'msg'=>'']);
			}
			return json(['code'=>0, 'data' => [['username' => $username]], 'msg'=>'']);
		}
	}

}
